var installedVersion = getPackageVersion("com.tencent.mm");
var supportVersion = ["8.0.35"]

console.info('当前微信版本：' + installedVersion)
if (supportVersion.includes(installedVersion)) {
  toast('当前微信版本为：' + installedVersion + '，满足需求')
  runScript()
} else {
  console.error('当前微信版本为：' + installedVersion + '，不满足需求，脚本停止')
  toast('当前微信版本为：' + installedVersion + '，不满足需求，脚本停止')
  exit()
}

function runScript() {
  begin();
  launchWechat();
  id('kd_').indexInParent(3).findOne(3000).click();
  console.info('打开我的页面');
  sleep(1000);
  const settings = pickup('设置');
  settings.clickBounds();
  sleep(2000);
  console.info('打开设置');
  scrollForward();
  sleep(3000);
  wait('切换帐号');

  const w = pickup('切换帐号');

  w.clickBounds();
  sleep(3000);
  console.info('点击切换帐号');
  if (wait('知道了')) {
    click('知道了', 0);
    sleep(1000);
    if (existsOne(id('kat').text('轻触头像以切换帐号'))) {
      console.info('切换帐号: %nickname%');
      if (existsOne(id('d1').text('%nickname%'))) {
        console.info('找到[%nickname%]微信号');
        id('d1').text('%nickname%').findOne(3000).parent().parent().parent().click();
        sleep(25000);
      }
      else if (existsOne(id('d1').text('%nickname% '))) {
        console.info('找到[%nickname%]微信号');
        id('d1').text('%nickname% ').findOne(3000).parent().parent().parent().click();
        sleep(25000);
      }
      else {
        toast('没有找到[%nickname%]微信号');
        console.warn('没有找到[%nickname%]微信号');
      }
    }
  }
  else {
    if (existsOne(id('kat').text('轻触头像以切换帐号'))) {
      console.info('切换帐号: %nickname%');
      if (existsOne(id('d1').text('%nickname%'))) {
        console.info('找到[%nickname%]微信号');
        id('d1').text('%nickname%').findOne(3000).parent().parent().parent().click();
        sleep(25000);
      }
      else if (existsOne(id('d1').text('%nickname% '))) {
        console.info('找到[%nickname%]微信号');
        id('d1').text('%nickname% ').findOne(3000).parent().parent().parent().click();
        sleep(25000);
      }
      else {
        toast('没有找到[%nickname%]微信号');
        console.warn('没有找到[%nickname%]微信号');
      }
    }
  }
  backToHome();
  end();
}

function launchWechat() {
  launch('com.tencent.mm');
  console.info('开始完全退出微信');
  waitForPackage('com.tencent.mm');
  backToHome();
  sleep(1000);
}

function backToHome() {
  let i = 0;
  while (!existsOne(id('f2s').text('通讯录')) || existsOne(id('text1').text('设置'))) {
    if (i >= 10) {
      launch('com.tencent.mm');
      console.info('开始完全退出微信');
      waitForPackage('com.tencent.mm');
    } else {
      back();
      sleep(1000);
      i++;
    }
  }
}

function begin() {
  device.wakeUp();
  device.keepScreenOn();
  auto();
}

function end() {
  toast('脚本执行结束');
  exit();
  // launch('com.timexxs.robot')
}

function getPackageVersion(packageName) {
  try {
      importPackage(android.content);
      var pckMan = context.getPackageManager();
      var packageInfo = pckMan.getPackageInfo(packageName, 0);
      return packageInfo.versionName;
  } catch (e) {
      dialogs.alert("您没有安装微信APP");
      exit();
  }
}
